//
//  MBSettingViewController.m
//  Meboo
//
//  Created by kyo on 2024/4/29.
//

#import "MBSettingViewController.h"
#import "KLMeCell.h"
#import "KLLoginViewController.h"
@interface MBSettingViewController ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic, strong) NSMutableArray *imageNameArr;
@property (nonatomic, strong) NSMutableArray *titleArr;
@property (nonatomic , strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIButton *leftBtn;
@end

@implementation MBSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self setupUI];
}

#pragma mark - Action

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)accountOut {
    [MBUserAccount deleteDoc];
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    //token失效重新登陆
    BaseNavViewController *nav = [[BaseNavViewController alloc] initWithRootViewController:[[KLLoginViewController alloc]init]];
    appDelegate.window.rootViewController = nav;
}

- (void)deleteAction {
    __WeakSelf
    //获取
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"Delete Account" message:@"Are you sure to delete the account? After deletion,you will not be able to retrieve all the account information" preferredStyle:UIAlertControllerStyleAlert];
    if ([self getIsIpad]){
        alert.popoverPresentationController.sourceView = self.view;
        alert.popoverPresentationController.sourceRect = CGRectMake(ScreenWidth/2,ScreenHeight/2,0,0);
        alert.popoverPresentationController.permittedArrowDirections = 0;
    }
    
    UIAlertAction  *emailAction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[MBMineDataManager sharedManager] logOut:^(BOOL success) {
            if (success) {
                [MBProgressHUD showSuccessMessage:@"delete success"];
                [weakSelf accountOut];
            }
        }];

        /**
         埋点 code
         删除账号 104
         */
        [[NSIOApiManager sharedManager] setIOWithCode:@"104" content:@"REMOVE_ACCOUNT"];
    }];
    
    UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:emailAction];
    [alert addAction:cancelAction];
    [self.currentViewController presentViewController:alert animated:YES completion:nil];
}


- (void)logOutAction {
    __WeakSelf
    //获取
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"Are You Sure To Log Out?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    if ([self getIsIpad]){
        alert.popoverPresentationController.sourceView = self.view;
        alert.popoverPresentationController.sourceRect = CGRectMake(ScreenWidth/2,ScreenHeight/2,0,0);
        alert.popoverPresentationController.permittedArrowDirections = 0;
    }
    
    UIAlertAction  *emailAction = [UIAlertAction actionWithTitle:@"Log Out" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [MBProgressHUD showSuccessMessage:@"logOut success"];
        [weakSelf accountOut];
    }];
    
    UIAlertAction  *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:emailAction];
    [alert addAction:cancelAction];
    [self.currentViewController presentViewController:alert animated:YES completion:nil];
}

#pragma mark -- collectionView  delegate datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titleArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KLMeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KLMeCell" forIndexPath:indexPath];
    cell.titleStr = self.titleArr[indexPath.row];
    cell.iconImageView.image = ImageWithName(self.imageNameArr[indexPath.row]);
    return cell;
}

//设置每个section的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(MBHeightScale(10), MBWidthScale(0), MBHeightScale(10), MBWidthScale(0));
}

//设置列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return MBHeightScale(0);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return MBWidthScale(0);
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ScreenWidth, MBHeightScale(60));
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        [self logOutAction];
    }else if (indexPath.row == 1) {
        [self deleteAction];
    }
}

#pragma mark - UI
- (void)setupUI {
    
    [self.view addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_top).offset(StatusBarAndNavigationBarHeight-MBHeightScale(8));
    }];
    
    [self.view addSubview:self.leftBtn];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(20));
        make.centerY.equalTo(self.titleLab);
        make.width.height.mas_equalTo(MBHeightScale(20));
    }];
    
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(StatusBarAndNavigationBarHeight);
        make.right.left.bottom.equalTo(self.view);
    }];
}

#pragma mark - Setter && Getter

- (NSMutableArray *)titleArr {
    if (!_titleArr) {
        _titleArr = [NSMutableArray arrayWithArray:@[@"Login out",@"Delete account"]];
    }
    return _titleArr;
}

- (NSMutableArray *)imageNameArr {
    if (!_imageNameArr) {
        _imageNameArr = [NSMutableArray arrayWithArray:@[@"me_login_loginout",@"me_delete_account"]];
    }
    return _imageNameArr;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _collectionView.hidden = YES;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[KLMeCell class] forCellWithReuseIdentifier:@"KLMeCell"];
    }
    return _collectionView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.font = PDSemiBoldScale(20);
        _titleLab.textColor = HEXS(@"333333");
        _titleLab.text = @"Settings";
    }
    return _titleLab;
}

- (UIButton *)leftBtn {
    if (!_leftBtn) {
        _leftBtn = [[UIButton alloc] init];
        [_leftBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_leftBtn setImage:ImageWithName(@"back_arrow_black") forState:0];
        [_leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.image = ImageWithName(@"setting_back_image");
    }
    return _backImageView;
}
@end
